package org.lanqiao.netctoss.controller.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.lanqiao.netctoss.controller.log4j.Log4JTestServlet;
import org.lanqiao.netctoss.entity.Admin;
import org.lanqiao.netctoss.entity.Permiss;
import org.lanqiao.netctoss.service.impl.AdminServiceImpl;
/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(LoginServlet.class);    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		//response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("GBK");
		String user  = request.getParameter("user");
		String psw = request.getParameter("psw");	
		HttpSession session = request.getSession();
/*		session.setAttribute("admin",adminBuss.login(new Admin(user,psw)));
		session.setAttribute("user", user);
		request.setAttribute("psw", psw);*/
		/*Admin admin = adminBuss.login(new Admin(user,psw));*/
		Admin admin = new AdminServiceImpl().login(new Admin(user,psw));
		if(admin==null){
			System.out.println("111333");
		}
	    List<Permiss> lp = admin.getLp() ;
	    /*for(Permiss p:lp){
	    	String url= p.getPer_url();
	    	String class1 =p.getPer_class();    	
	    }*/
		if (admin !=null){
			session.setAttribute("admin",admin);
		/*	session.setAttribute("user", user);
			session.setAttribute("psw", psw);*/
			RequestDispatcher rd =request.getRequestDispatcher("/view/index.jsp");
			rd.forward(request, response);
		}else{
			PrintWriter out = response.getWriter();
			out.print("<script>alert('帐号或用户名错误,请重新登录!');window.location.href='login.jsp'</script>");			
			/*RequestDispatcher rd =request.getRequestDispatcher("login.jsp");
			rd.forward(request, response);*/
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
